package com.dhh.toodledo.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dhh.toodledo.R;
import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.model.Task;
import com.dhh.toodledo.util.AuthUtil;
import com.dhh.toodledo.util.HttpUtil;
import com.flurry.android.FlurryAgent;

import greendroid.app.GDActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ShowPriorityTaskActivity extends GDActivity  implements View.OnClickListener{
	private AuthUtil au;
	private Map<String,Integer> sumMap = new HashMap<String,Integer>();
	private List<Task> tasks;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.priority_task);
        au = new AuthUtil(getSharedPreferences("myPre", 0));
        sumMap.put("negative", 0);
        sumMap.put("low", 0);
        sumMap.put("medium", 0);
        sumMap.put("high", 0);
        sumMap.put("top", 0);
        
        initView();
        initActionBar();
        new SearchThread().start();
	}
	
	@Override
    public void onStart(){
       super.onStart();
       FlurryAgent.onStartSession(this, "2ND7X5T2HQCRXGXJ1BUF");
    }
    
    @Override
    public void onStop(){
       super.onStop();
       FlurryAgent.onEndSession(this);
    }

	@Override
	public void onClick(View view) {
		Intent intent = new Intent(ShowPriorityTaskActivity.this,ShowAllTaksActivity.class);
		if(view.getId()==R.id.pt_negative){
			intent.putExtra("showType", "negative");
		}else if(view.getId()==R.id.pt_low){
			intent.putExtra("showType", "low");
		}else if(view.getId()==R.id.pt_medium){
			intent.putExtra("showType", "medium");
		}else if(view.getId()==R.id.pt_high){
			intent.putExtra("showType", "high");
		}else if(view.getId()==R.id.pt_top){
			intent.putExtra("showType", "top");
		}
		this.startActivity(intent);
	}
	
	private void initView(){
		findViewById(R.id.pt_negative).setOnClickListener(this);
		findViewById(R.id.pt_low).setOnClickListener(this);
		findViewById(R.id.pt_medium).setOnClickListener(this);
		findViewById(R.id.pt_high).setOnClickListener(this);
		findViewById(R.id.pt_top).setOnClickListener(this);
	}
	
	
	public void initActionBar(){
		getActionBar().setTitle("Priority");
		getActionBar().setGravity(Gravity.CENTER_HORIZONTAL);
        getActionBar().setBackgroundColor(Color.rgb(9, 135, 205));
        getActionBar().getChildAt(0).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ShowPriorityTaskActivity.this,TDAActivity.class);
				startActivity(intent);
			}});
	}
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	displaySearchResults();
        	ShowPriorityTaskActivity.this.findViewById(R.id.td_loading).setVisibility(View.GONE);
        	ShowPriorityTaskActivity.this.findViewById(R.id.dd_content).setVisibility(View.VISIBLE);
        	
        }
    };
    
    public void displaySearchResults(){
		((TextView)findViewById(R.id.pt_nagative_n)).setText(String.valueOf(sumMap.get("negative")));
		((TextView)findViewById(R.id.pt_low_n)).setText(String.valueOf(sumMap.get("low")));
		((TextView)findViewById(R.id.pt_medium_n)).setText(String.valueOf(sumMap.get("medium")));
		((TextView)findViewById(R.id.pt_high_n)).setText(String.valueOf(sumMap.get("high")));
		((TextView)findViewById(R.id.pt_top_n)).setText(String.valueOf(sumMap.get("top")));
	}
    private Handler eHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(ShowPriorityTaskActivity.this,LoginActivity.class);
        	ShowPriorityTaskActivity.this.startActivity(intent);
        }
    };
    
    private Handler nHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(ShowPriorityTaskActivity.this,TDAActivity.class);
        	ShowPriorityTaskActivity.this.startActivity(intent);
        }
    };
    
	private class SearchThread extends Thread {
        @Override
        public void run() {  
        	try {
				tasks = HttpUtil.getAllTasks(au.getKeyValue());
				int negative = 0;
	            int low = 0;
	            int medium = 0;
	            int high = 0;
	            int top = 0;
	        	//Negative
	    		for(Task t : tasks){
	    			if(t.getPriority()!=null && t.getPriority()==-1){
	    				negative++;
	    			}
	    		}
	    		
	    		//Low
	    		for(Task t : tasks){
	    			if(t.getPriority()!=null && t.getPriority()==0){
	    				low++;
	    			}
	    		}
	    		
	    		//Medium
	    		for(Task t : tasks){
	    			if(t.getPriority()!=null && t.getPriority()==1){
	    				medium++;
	    			}
	    		}
	    		
	    		//High
	    		for(Task t : tasks){
	    			if(t.getPriority()!=null && t.getPriority()==2){
	    				high++;
	    			}
	    		}
	    		
	    		//Top
	    		for(Task t : tasks){
	    			if(t.getPriority()!=null && t.getPriority()==3){
	    				top++;
	    			}
	    		}

	    		sumMap.put("negative", negative);
	            sumMap.put("low", low);
	            sumMap.put("medium", medium);
	            sumMap.put("high", high);
	            sumMap.put("top", top);
	        	
	        	handler.sendEmptyMessage(0);
			} catch (InValidAccountException e) {
				eHandler.sendEmptyMessage(0);
			} catch (Exception e) {
				nHandler.sendEmptyMessage(0);
			}
        }        
    }

}
